#include "widget.h"
#include "ui_widget.h"
#include"menu.h"
#include"menulist.h"
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    init();
    nsize=1;
    nsizeok=1;
}

void Widget::init(){
    setWindowFlags(Qt::FramelessWindowHint); //隐藏窗体头
    gettonet = new Getinftonet(this);
    ui->widget_3->init(gettonet);
    ui->widget_7->init(gettonet);

    //参数初始化
    connect(ui->widget_3,SIGNAL(siggetinftochick()),gettonet,SLOT(slotshopinf_getf()));
    connect(gettonet,SIGNAL(shopinfimg(QByteArray *)),ui->widget_3,SLOT(drowshopinfimg(QByteArray*))); //商家图片展是

    connect(ui->widget_3,SIGNAL(sigchangeislog(QString&)),this,SLOT(slotchangeislog(QString &))); //在线状态改变

    /* 发送添加商品请求 */
    connect(gettonet,SIGNAL(getshopinftojson(structshopinf *)),ui->widget_3,SLOT(getshopinfo(structshopinf*)));
    connect(gettonet,SIGNAL(sigmenus(QVector<mymenu> &)),SLOT(slotaddmenu(QVector<mymenu> &)));


    connect(gettonet,SIGNAL(sigmenulist(clihnum &)),SLOT(slotmenulist(clihnum &)));//订单




}

Widget::~Widget()
{
    QByteArray buf;
    buf.append(QString("{"
                       "\"unit\":\"%1\","
                       "\"IsLogin\":\"%2\"}").arg(shopunit).arg("离线"));
    gettonet->topost("http://hunya.fun:9000/cliupshopinf",buf);
    delete ui;
}


void Widget::showshopinf(structshopinf*){

}

///窗体拖动--------
void Widget::mousePressEvent(QMouseEvent *event)
{
    bPressFlag = true;
    beginDrag = event->pos();
    QWidget::mousePressEvent(event);
}

void Widget::mouseReleaseEvent(QMouseEvent *event)
{
    bPressFlag = false;
    QWidget::mouseReleaseEvent(event);
}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
    if(bPressFlag)
    {
        QPoint relaPos(QCursor::pos() - beginDrag);
        move(relaPos);
    }
    QWidget::mouseMoveEvent(event);
}

void Widget::on_close_btn_clicked()
{
    QCoreApplication::quit();

}

void Widget::on_min_btn_2_clicked()
{
    // showMaximized();
}

void Widget::on_max_btn_2_clicked()
{
    showMinimized();
}


void Widget::on_comboBox_2_currentIndexChanged(const QString &arg1)
{

    QByteArray buf;
    buf.append(QString("{"
                       "\"unit\":\"%1\","

                       "\"IsLogin\":\"%2\"}").arg(shopunit).arg(arg1));
    //gettonet->topost();
}

void Widget::on_comboBox_currentIndexChanged(const QString &arg1)
{
    if(Islog == arg1) return;
    QByteArray buf;
    buf.append(QString("{"
                       "\"unit\":\"%1\","
                       "\"IsLogin\":\"%2\"}").arg(shopunit).arg(arg1));
    gettonet->topost("http://hunya.fun:9000/cliupshopinf",buf);
    ui->comboBox->setCurrentText(arg1);
    gettonet->getshopinftosend();
}
void Widget::slotchangeislog(QString &islog){

    Islog = islog;
    ui->comboBox->setCurrentText(islog);
}


void Widget::slotaddmenu(QVector<mymenu> &menulist)//菜单页面
{

    QVector<mymenu>::iterator it =  menulist.begin();

    QWidget *widg = new QWidget;
    QVBoxLayout *layout = new QVBoxLayout;
    for(it;it!=menulist.end();it++){
        menu *menut  = new menu;
        menut->init(*it);
        menut->resize(480,200);
        layout->addWidget(menut);
    }

    widg->setLayout(layout);
    widg->resize(520,menulist.count()*200);
    ui->scrollArea->setWidget(widg);


}

void  Widget::slotmenulist(clihnum & clih){
    {
        if(clih.menus.count()==0)return;
        menulist *mend = new menulist(0);
//        connect(mend,&menulist::sigmlistdel,[&]{
//            nsize--;
//            widgd->resize(420,widgd->size().height()-200);

//        });
connect(mend,SIGNAL(sigmlistdel(clihnum &)),SLOT(slotmenulistok(clihnum &)));
        mend->init(clih,gettonet);
        widgd->resize(420,200*nsize);
        layoutd->addWidget(mend);

        widgd->setLayout(layoutd);
nsize++;
        ui->scrollArea_2->setWidget(widgd);
        }


}

void  Widget::slotmenulistok(clihnum & clih){
    {
                    nsize--;
                    widgd->resize(420,widgd->size().height()-200);
        if(clih.menus.count()==0)return;
        menulist *mend = new menulist(0);

        mend->init(clih,gettonet,true);
        widgd3->resize(420,200*nsizeok);
        layoutd3->addWidget(mend);

        widgd3->setLayout(layoutd3);
nsizeok++;
        ui->scrollArea_3->setWidget(widgd3);
        //ui->scrollArea_2->resize(620,200+300*nsize);

        }

}







void Widget::on_tabWidget_tabBarClicked(int index)
{

    if(index == 2){
gettonet->getshopinfuptosend();
    }
}
